package com.lvyq.myplayer.adapter.search

/**
 * 文件描述：SearchAdapter
 * 作者：lvyq
 * 创建时间：2025-05-07
 * 修改时间：2025-05-07
 */
import androidx.recyclerview.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
 import com.lvyq.myplayer.model.index.IndexHotSongBean
import com.lvyq.myplayer.parcelable.SongInfoData

class SearchAdapter(  private val itemClickListener: SearchAdapter.OnItemClickListener) :
    RecyclerView.Adapter<SearchAdapter.ViewHolder>() {
    private var items: List<SongInfoData>?=null
    lateinit var resultTextView:TextView

    interface OnItemClickListener {
        fun onItemClick( position: Int,data: SongInfoData)
    }



    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(android.R.layout.simple_list_item_1, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        items?.let {
            var songInfoData = it[position]
            (holder as ViewHolder).bind(songInfoData)
            holder.itemView.setOnClickListener {
                itemClickListener.onItemClick(position, songInfoData)
            }
        }


    }

    override fun getItemCount() = items?.size?:0

    fun updateData(newData: IndexHotSongBean) {
        items = newData.data
        notifyDataSetChanged()
    }

    inner  class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {


        init {
            resultTextView = view.findViewById<TextView>(android.R.id.text1)

        }

        fun bind(songInfoData: SongInfoData) {
            resultTextView.text=songInfoData.title+"-"+songInfoData.singer
        }
    }
}